package com.example.demo.service.impl;

import com.example.demo.mapper.BaseMapper;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by liujiahao on 2019/4/2.
 */
@Service
public class StudentServiceImpl implements StudentService{
    @Autowired
    private StudentMapper mapper;
    @Autowired
    BaseMapper baseMapper;
    @Transactional()
    @Override
    public List<Map<String, String>> findById(String name) {
        System.out.println(mapper.findAll());

        if(name.equals("ljh")){
           // mapper.update();
            try {
                Thread.sleep(7000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            throw new  RuntimeException("SSSS");
        }else{
            mapper.update();
        }

        System.out.println("当前线程"+Thread.currentThread().getName()+"user"+name+"结束");
        /*if(name.equals("ljh2")){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }*/

        return null;
    }
    @Transactional
    @Override
    public String test() {
        //耗时操作1
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        //耗时操作2
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                throw new RuntimeException("ASDA");
            }
        }).start();
        return "yes";
    }

    @Transactional
    @Override
    public void insert() {
        baseMapper.insert(10,"555");
        //if(true)throw new RuntimeException();
    }
}
